package com.sx.frame.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sx.frame.common.constant.CommonConst;
import lombok.Data;

import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 成功标志
     */
    private boolean flag = true;

    /**
     * 返回处理消息
     */
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    private T result;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    //兼容2.0

    private String jobId;

    private String errorMessage;

    private Object CommonConstdata;

    public Result() {

    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = CommonConst.SC_OK_200;
        this.success = true;
        this.flag = true;
        return this;
    }

    @Deprecated
    public static Result<Object> deprecatedOk() {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConst.SC_OK_200);
        r.setMessage("成功");
        r.setFlag(true);

        return r;
    }

    @Deprecated
    public static Result<Object> deprecatedOk(String msg) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setFlag(true);
        r.setCode(CommonConst.SC_OK_200);
        r.setMessage(msg);
        return r;
    }

    @Deprecated
    public static Result<Object> deprecatedOk(Object data) {
        Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setSuccess(true);
        r.setCode(CommonConst.SC_OK_200);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> OK() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setSuccess(true);
        r.setCode(CommonConst.SC_OK_200);
        r.setMessage("成功");
        return r;
    }

    public static <T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setFlag(true);
        r.setCode(CommonConst.SC_OK_200);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> OK(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setFlag(true);
        r.setCode(CommonConst.SC_OK_200);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static Result<Object> error(String msg) {
        return error(CommonConst.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

    public static <T> Result<T> error(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setFlag(false);
        r.setSuccess(false);
        r.setCode(CommonConst.SC_INTERNAL_SERVER_ERROR_500);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    /**
     * 返回值200，业务错误
     *
     * @param msg
     * @return
     */
    public static Result<Object> errorWithCodeOK(String msg) {
        Result<Object> r = new Result<>();
        r.setCode(CommonConst.SC_OK_200);
        r.setMessage(msg);
        r.setFlag(false);
        r.setSuccess(false);
        return r;
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setFlag(false);
        r.setSuccess(false);
        return r;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = CommonConst.SC_INTERNAL_SERVER_ERROR_500;
        this.flag = false;
        this.success = false;
        return this;
    }

    public Result<T> error400(String message) {
        this.message = message;
        this.code = CommonConst.SC_INTERNAL_SERVER_ERROR_400;
        this.flag = false;
        this.success = false;
        return this;
    }

    /**
     * 无权限访问返回结果
     */
    public static Result<Object> noauth(String msg) {
        return error(CommonConst.SC_JEECG_NO_AUTHZ, msg);
    }

    @JsonIgnore
    private String onlTable;

}
